package com.yanqu.road.server.gameplayer.module.activity;

import com.yanqu.road.entity.activity.ActivityDisperseGoldInfo;
import com.yanqu.road.entity.activity.ActivityDisperseGoldRecord;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.player.UserActivityDisperseGoldBussiness;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.logic.pb.ActivityDisperseGoldPb;
import com.yanqu.road.pb.activity.ActivityDisperseGoldProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.ActivityDisperseGoldMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ActivityDisperseGoldModule extends GeneralModule {

    private Map<Long, ActivityDisperseGoldRecord> recordMap = new ConcurrentHashMap<>();

    public ActivityDisperseGoldModule(GamePlayer player) {
        super(player);
    }

    private Long currentDisperseId = 0L;

    @Override
    public boolean loadData() {
        ActivityDisperseGoldRecord disperseGoldRecord = UserActivityDisperseGoldBussiness.getMaxDisperseGoldRecordMap(player.getUserId());
        if(disperseGoldRecord != null){
            currentDisperseId = disperseGoldRecord.getDisperseId();
        }
        return true;
    }

    @Override
    public boolean afterLoadData() {

        return true;
    }

    @Override
    public boolean saveData() {
        for(ActivityDisperseGoldRecord goldRecord : recordMap.values()){
            if(goldRecord.isInsertOption()){
                UserActivityDisperseGoldBussiness.addDisperseGoldRecord(goldRecord);
            }
        }
        return true;
    }

    @Override
    public void loginSendMsg() {

    }

    @Override
    public void afterLogin() {
        List<ActivityDisperseGoldInfo> goldInfos = ActivityDisperseGoldMgr.needSendDisperseGold();
        if (goldInfos == null) {
            return;
        }
        for(ActivityDisperseGoldInfo goldInfo : goldInfos){
            if(ActivityDisperseGoldMgr.isDisperseGoldEnd(goldInfo)){
                continue;
            }
            ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(goldInfo.getActivityId());
            if(activityInfo == null) {
                continue;
            }
            //活动结束前30秒，不推送给玩家
            long nowTime = System.currentTimeMillis() / 1000;
            if(activityInfo.getEndShowTime() - 30 < nowTime || ActivityDisperseGoldMgr.isDisperseGoldOutSendTime(goldInfo, 10)){
                continue;
            }
            if(goldInfo.getDisperseId() <= currentDisperseId){
                continue;
            }
            ActivityDisperseGoldRecord record = player.getModule(ActivityDisperseGoldModule.class).getActivityDisperseGoldRecord(goldInfo.getDisperseId());
            if(null != record){
                continue;
            }

            if(goldInfo.getGroupId() > 0) {
                int groupId = 0;
                if (UnionActivityGroupHelper.isGroupActivityType(activityInfo.getType())) {
                    groupId = GameUnionActivityGroupMgr.getGroupId(activityInfo.getActivityId(), player.getUserId());
                }
                //无分组，只领取第一分组
                if(groupId == UnionActivityGroupHelper.NO_GROUP_ID && goldInfo.getGroupId() > 1){
                    continue;
                }
                if(groupId != goldInfo.getGroupId()){
                    continue;
                }
            }

            ActivityDisperseGoldProto.ActivityDisperseGoldClientRespMsg.Builder reqMsg = ActivityDisperseGoldProto.ActivityDisperseGoldClientRespMsg.newBuilder();

            ActivityDisperseGoldProto.ActivityDisperseGoldInfoTemp.Builder builder = ActivityDisperseGoldPb.parseToDisperseGoldInfoTemp(goldInfo);
            //花石纲活动需要活动期间都展示动画 需要告诉客户端散财结束时间
            if (activityInfo.getType() == eActivityType.CrossHuaShiGangActivity.getValue()) {
                builder.setEndTime(activityInfo.getEndShowTime() * 1000);
            } else if (activityInfo.getType() == eActivityType.Tomb.getValue()) { //古墓活动，活动期间都要展显动画
                builder.setEndTime(activityInfo.getEndShowTime() * 1000);

            }
            reqMsg.setGoldInfo(builder);
            player.sendPacket(Protocol.U_ACTIVITY_DISPERSE_GOLD_SYNC, reqMsg);
        }
    }

    /**
     * 记录领取散财信息
     * @param disperseId
     */
    public void setRecord(long disperseId) {
        synchronized (recordMap) {
            if (currentDisperseId < disperseId && !recordMap.containsKey(disperseId)) {
                currentDisperseId = disperseId;
                ActivityDisperseGoldRecord record = new ActivityDisperseGoldRecord(player.getUserId(), disperseId);
                record.setInsertOption();
                recordMap.put(disperseId, record);
            }
        }
    }

    public ActivityDisperseGoldRecord getActivityDisperseGoldRecord(long disperseId){
        return recordMap.get(disperseId);
    }
}
